﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using ECRMS.WinForm.WinService;
using DevExpress.XtraGrid.Views.Base;
using System.Linq;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;

namespace ECRMS.WinForm.ModuleEdits
{
    public partial class  StructureList: BaseEdit
    {
        public Core core = Core.getInstance();
        public StructureList(Rectangle r)
        {
            InitializeComponent();
            InitView( r);
            InitData();
        }

        private void InitData(bool isPostBack=false)
        {
            if (!isPostBack)
            {
                FuncResult result = core.CoreStructure.GetStructures(core.CoreUser.UserInfo.CorporationRefId);
                if (result.Status == 0)
                {
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                    return;
                }
            }
            gdStructure.DataSource = core.CoreStructure.Structures;
            gdStructure.RefreshDataSource();
        }

        private void InitView(Rectangle r)
        {
            this.Left = r.Left + (r.Width - this.Width) / 2;
            this.Top = r.Top + (r.Height - this.Height) / 2;
            this.Text = "职位列表";

            base.NewBarButtonVisiable = true;
            base.SaveBarButtonVisiable = false;
            base.EditBarButtonVisiable = true;
            base.DelBarButtonVisiable = false;
            base.RefreshBarButtonVisiable = true;
            base.ResetBarButtonVisiable = false;
            base.OtherBarButtonVisiable = false;
            base.Other1ButtonVisiable = false;
        }
        //新增部门
        public override void newBarButtonClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.newBarButtonClick(sender, e);
            core.CoreStructure.OperatorRow = new CtStructure
            {
                StructureRefId = Guid.NewGuid(),
                CorporationRefId=core.CoreUser.UserInfo.CorporationRefId
            };
            StructureEdit list = new StructureEdit(new Rectangle(this.Left, this.Top, this.Width, this.Height), OperationEnum.New);

            DialogResult result = list.ShowDialog();
            if (result == DialogResult.OK)
            {
                InitData(true);
            }
            else if(result==DialogResult.No)
            {
            }
            core.CoreStructure.OperatorRow = null;
            list.Dispose();
        }
        public override void editBarButtonClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.editBarButtonClick(sender, e);
            Guid structureId = core.CoreCommon.GetGridFocusRowRefId(gdStructure, gridView1, "StructureRefId");
            if (structureId != Guid.Empty && structureId!=null)
            {
                core.CoreStructure.OperatorRow = core.CoreStructure.Structures.FirstOrDefault(p => p.StructureRefId == structureId);
            }
            else
            {
                XtraMessageBox.Show(this,"请选择职位");
                return;
            }
            StructureEdit list = new StructureEdit(new Rectangle(this.Left, this.Top, this.Width, this.Height), OperationEnum.Modify);

            DialogResult result = list.ShowDialog();
            if (result == DialogResult.OK)
            {
                InitData(true);
            }
            else if (result == DialogResult.Cancel)
            {
                //todo
            }
            //清空编辑项
            core.CoreStructure.OperatorRow = null;
            list.Dispose();
        }
        //双击选择职位
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            Guid structureId = core.CoreCommon.GetGridFocusRowRefId(gdStructure, gridView1, "StructureRefId");
            if (structureId != null)
            {
                core.CoreEmployee.OperatorRow.UserInfo.StructureRefId = structureId;
                core.CoreEmployee.OperatorRow.UserInfo.StructureName = core.CoreStructure.Structures.FirstOrDefault(p=>p.StructureRefId==structureId).StrutureName;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}